<?
/**
  * Locate IP PHP Class
  * v.000000001
  * By Harper Reed
  * Harper@nata2.org
  * I am pretty awesome
  * 
  * I hate documenting my code. I cannot lie. But its pretty easy. 
  * I will make more documentation later. Basically you just do this:
  * 
  *
  * -----------------------------------------------------------
  *
  * Copyright 2006 Harper Reed
  * 
  * -----------------------------------------------------------
  *
  * LICENSE
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License (GPL)
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * To read the license please visit http://www.gnu.org/copyleft/gpl.html
  *
  */

/**
 * This is the XML class. I have no idea what or where it is 
 * or coming from. I think i got it from the phpflickr class.
 * i don't know
 *  
 */
require_once('xml.php');

class locateIP{
	

	function locateIP(){
	}
	
	function doLookup(){
		$ip = $_SERVER['REMOTE_ADDR'];
		$data = $this->doHostipApiCall($ip);
		return $data;
	}
	
	function getZip($city, $state,$lat, $lng){
	$lng = substr($lng,0,6);$lng = abs($lng);
	$lat = substr($lat,0,4);$lat = abs($lat);
	$q = "select zip from zips where name = '".strtoupper($city)."' and state = '".strtoupper($state)."' and lng like '".$lng."%' and lat like '".$lat."%'  limit 1;";
	$r = new Query($q);
	$zip =$r->getAssoc();
	return $zip["zip"];
	}
	
	function getZipStateCity($city, $state){
	$q = "select zip from zips where name = '".strtoupper($city)."' and state = '".strtoupper($state)."' and lng limit 1;";
	$r = new Query($q);
	$zip =$r->getAssoc();
	return $zip["zip"];
	}
	
	function doHostipApiCall($ip){
		$apiurl = "http://api.hostip.info/?ip=".$ip;
		$xmldata = file_get_contents($apiurl);
		$this->xml_parser = new xml(false, true, true);
		$parseddata = $this->xml_parser->parse($xmldata);
		$data["country"] = $parseddata["HostipLookupResultSet"]["gml:featureMember"]["Hostip"]["countryName"];
		$data["country_abbr"] = $parseddata["HostipLookupResultSet"]["gml:featureMember"]["Hostip"]["countryAbbrev"];
		$place = split(",",$parseddata["HostipLookupResultSet"]["gml:featureMember"]["Hostip"]["gml:name"]);
		$data["city"] = str_replace(" ","",$place[0]);
		$data["state"] = str_replace(" ","", $place[1]);
		$latlng = split(",",$parseddata["HostipLookupResultSet"]["gml:featureMember"]["Hostip"]["ipLocation"]["gml:PointProperty"]["gml:Point"]["gml:coordinates"]);
		$data["lng"] = $latlng[0];
		$data["lat"] = $latlng[1];
		$data["zip"] = $this->getZip($data["city"],$data["state"],$data["lat"],$data["lng"]);
		if ($data["zip"] == "") $data["zip"] = $this->getZipStateCity($data["city"],$data["state"]);
		return $data;
	}

}
